from django.db import models

# Create your models here.
from DjangoUeditor.models import UEditorField
from users.models import UserProfile


class Category(models.Model):

    parent_category = models.ForeignKey('self', models.DO_NOTHING, related_name="categorise", blank=True, null=True,)
    parent_category = models.ForeignKey('self', models.DO_NOTHING, related_name="categories", blank=True, null=True,
                                        verbose_name="父级分类")
    name = models.CharField(max_length=10, null=False, verbose_name="分类名")
    ordered = models.IntegerField(null=False, default=0, verbose_name="排序")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")

    class Meta:
        db_table = 'category'
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(max_length=50, null=False, verbose_name="标题")
    category = models.ForeignKey(Category, models.DO_NOTHING, related_name="articles", db_column='category', null=False,
                                 verbose_name="类别")
    author = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="articles", db_column='author',
                               verbose_name="作者")

    image = models.ImageField(max_length=100, null=False, upload_to='article_images/', verbose_name="图片")

    image = models.ImageField(null=False, upload_to='article_images/', verbose_name="图片")

    content = UEditorField(imagePath="article/images/", width=1000, height=300,
                           filePath="article/files/", default='', verbose_name="内容")
    is_auth = models.NullBooleanField(blank=True, null=True, default=None,
                                      choices=((None, '未审核'), (True, '已通过'), (False, '未通过')), verbose_name="审核")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    is_delete = models.NullBooleanField(null=True, verbose_name="删除")
    click_count = models.IntegerField(null=True, default=0, verbose_name="点击量")
    praise_count = models.IntegerField(null=True, default=0, verbose_name="点赞量")
    comment_count = models.IntegerField(null=True, default=0, verbose_name="评论量")
    collect_count = models.IntegerField(null=True, default=0, verbose_name="收藏量")
    inform_count = models.IntegerField(null=True, default=0, verbose_name="举报量")

    class Meta:
        db_table = 'article'
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class CollectArticle(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="collect_articles", null=False,
                             verbose_name="收藏者")
    article = models.ForeignKey(Article, models.DO_NOTHING, related_name="collect_articles", null=False,
                                verbose_name="文章")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="收藏时间")

    class Meta:
        unique_together = (('user', 'article'),)
        db_table = 'collect_article'
        verbose_name = '收藏文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}收藏了{self.article.title}'


class CommentArticle(models.Model):
    article = models.ForeignKey(Article, models.DO_NOTHING, related_name="comment_articles", null=False,
                                verbose_name="文章")
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="comment_articles", null=False,
                             verbose_name="评论者")
    pid = models.ForeignKey('self', related_name="comments", null=True, blank=True)

    content = models.CharField(max_length=250, null=False, verbose_name="内容")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")

    class Meta:
        db_table = 'comment_article'
        verbose_name = '评论文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}评论了{self.article.title}'


class InformArticle(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="inform_articles", null=False,
                             verbose_name="举报者")
    article = models.ForeignKey(Article, models.DO_NOTHING, related_name="inform_articles", null=False,
                                verbose_name="文章")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="举报时间")

    class Meta:
        db_table = 'inform_article'
        unique_together = (('user', 'article'),)
        verbose_name = '举报文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}举报了{self.article.title}'


class PraiseArticle(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="praise_articles", null=False,
                             verbose_name="点赞者")
    article = models.ForeignKey(Article, models.DO_NOTHING, related_name="praise_articles", null=False,
                                verbose_name="文章")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")

    class Meta:
        db_table = 'praise_article'
        unique_together = (('user', 'article'),)
        verbose_name = '点赞文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}点赞了{self.article.title}'


class Race(models.Model):
    race_category = models.ForeignKey(Category, models.DO_NOTHING, db_column='race_category', verbose_name="赛事种类")
    begin = models.DateTimeField(null=False, verbose_name="开始时间")
    end = models.DateTimeField(blank=True, null=True, verbose_name="结束时间")
    team_a = models.CharField(max_length=50, blank=True, null=True, verbose_name="主场球队")
    point_a = models.SmallIntegerField(blank=True, null=True, verbose_name="主队分数")
    team_b = models.CharField(max_length=50, blank=True, null=True, verbose_name="客场球队")
    point_b = models.SmallIntegerField(blank=True, null=True, verbose_name="客队分数")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")

    class Meta:
        db_table = 'race'
        verbose_name = '赛事'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.team_a}对阵{self.team_b}'
